<?php

require __DIR__.'/db.php';

class Article
{
    /**
     * 数据库句柄
     * @var
     */
    private $_db;

    /**
     * 构造方法
     * Article constructor.
     * @param $_db
     */
    public function __construct($_db)
    {
        $this->_db = $_db;
    }

    /**
     * 创建文章
     * @param $title
     * @param $content
     * @param $userId
     */
    public function create($title,$content,$userId)
    {
        if( empty($title) ){
            throw new Exception('文章标题不能为空',ErrorCode::ARTICLE_TITLE_CANNOT_EMPTY);
        }
        if( empty($content) ){
            throw new Exception('文章内容不能为空',ErrorCode::ARTICLE_CONTENT_CANNOT_EMPTY);
        }

        $sql = 'insert into `article` (`title`,`content`,`user_id`,`created_at`) values(:title,:content,:user_id,:created_at)';

        $date_now = date('Y-m-d H:i:s');
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':title',$title);
        $stmt->bindParam(':content',$content);
        $stmt->bindParam(':user_id',$userId);
        $stmt->bindParam(':created_at',$date_now);
        if( !$stmt->execute() ){
            throw new Exception('发表文章失败',ErrorCode::ARTICLE_CREATED_FAIL);
        }

        return [
            'articleId' => $this->_db->lastInsertId(),
            'title' => $title,
            'content' => $content,
            'userId' => $userId,
            'created_at' => $date_now
        ];
    }

    /**
     * 查看一篇文章
     * @param $articleId
     * @return mixed
     * @throws Exception
     */
    public function view($articleId)
    {
        if( empty($articleId) ){
            throw new Exception('文章iD不能为空',ErrorCode::ARTICLE_ID_CANNOT_EMPTY);
        }

        $sql = 'select * from `article` where `article_id` = :id';
        $stmt = $this->_db->prepare($sql);

        $stmt->bindParam(':id',$articleId);
        $stmt->execute();
        $article = $stmt->fetch(PDO::FETCH_ASSOC);

        if( empty($article) ){
            throw new Exception('文章不存在',ErrorCode::ARTICLE_NOT_FOUND);
        }

        return $article;
    }

    /**
     * 编辑文章
     * @param $articleId
     * @param $title
     * @param $content
     * @param $userId
     * @return array|mixed
     * @throws Exception
     */
    public function edit($articleId,$title,$content,$userId)
    {
        $article = $this->view($articleId);
        if( $article['user_id'] !== $userId ){
            throw new Exception('您无权编辑该文章',ErrorCode::PERMISSION_DENIED);
        }

        $title = empty($title) ? $article['title'] : $title;
        $content = empty( $content )?$article['content'] : $content;

        if( $title === $article['title'] && $content === $article['content'] ){
            return $article;
        }

        $sql = 'update `article` set `title` = :title,`content` = :content where `article_id` = :id';

        $stmt = $this->_db->prepare($sql);
        $data = [
            ':title' => $title,
            ':content' => $content,
            ':id' =>$articleId
        ];

        if(!$stmt->execute($data)){
            throw new Exception('文章编辑失败',ErrorCode::ARTICLE_EDIT_FAIL);
        }

        return [
            'article_id' => $articleId,
            'title' => $title,
            'content' => $content,
            'created_at' => $article['created_at']
        ];

    }

    /**
     * 删除文章
     * @param $articleId
     * @param $userId
     */
    public function delete($articleId,$userId)
    {
        $article = $this->view($articleId);
        if( $article['user_id'] !== $userId ){
            throw new Exception('您无权操作',ErrorCode::PERMISSION_DENIED);
        }
        $sql = 'delete from `article` WHERE `article_id` = :article_id AND `user_id` = :user_id';
        $stmt = $this->_db->prepare($sql);
        $stmt ->bindParam(':article_id' ,$articleId);
        $stmt->bindParam(':user_id',$userId);
        if( !$stmt->execute() ){
            throw new Exception('删除失败',ErrorCode::ARTICLE_DELETE_FAIL);
        }

        return true;
    }

    /**
     * 获取文章列表
     * @param $userId
     * @param int $page
     * @param int $size
     * @return mixed
     * @throws Exception
     */
    public function getList($userId,$page=1,$size=10)
    {
        if( $size > 100 ){
            throw new Exception('分页大小最大为100',ErrorCode::PAGE_SIZE_TOO_BIG);
        }

        $sql = 'select * from `article` where `user_id` = :user_id limit :limit,:offset';
        $limit = ($page - 1) * $size;
        $limit  = $limit < 0 ? 0 :$limit;
        $stmt = $this->_db->prepare($sql);

        $data = [
            ':user_id' => $userId,
            ':limit' => $limit,
            ':offset' => $size
        ];


        $stmt->execute($data);
        $info = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $info;
    }
}